package android.slc.box.db.sp;

import java.util.Set;

/**
 * @author slc
 * @date 2023/9/8
 **/
public interface DataPersistence<T extends DataPersistence<T>> {

    default int getInt(String key) {
        return getInt(key, -1, false);
    }

    default int getInt(String key, int defValue) {
        return getInt(key, defValue, true);
    }

    int getInt(String key, int defValue, boolean autoSaveDefValue);

    default long getLong(String key) {
        return getLong(key, -1, false);
    }

    default long getLong(String key, long defValue) {
        return getLong(key, defValue, true);
    }

    long getLong(String key, long defValue, boolean autoSaveDefValue);

    default float getFloat(String key) {
        return getFloat(key, -1f, false);
    }

    default float getFloat(String key, float defValue) {
        return getFloat(key, defValue, true);
    }

    float getFloat(String key, float defValue, boolean autoSaveDefValue);

    default boolean getBoolean(String key) {
        return getBoolean(key, false, false);
    }

    default boolean getBoolean(String key, boolean defValue) {
        return getBoolean(key, defValue, true);
    }

    boolean getBoolean(String key, boolean defValue, boolean autoSaveDefValue);

    default String getString(String key) {
        return getString(key, "");
    }

    default String getString(String key, String defValue) {
        return getString(key, defValue, defValue != null);
    }

    String getString(String key, String defValue, boolean autoSaveDefValue);

    default Object getValue(String key) {
        return getValue(key, null);
    }

    default Object getValue(String key, Object defValue) {
        return getValue(key, defValue, defValue != null);
    }

    Object getValue(String key, Object defValue, boolean autoSaveDefValue);


    T putValue(String key, Object value);

    default T putIfAbsent(String key, Object value) {
        Object resultValue = getValue(key, null);
        if (resultValue == null) {
            putValue(key, value);
        }
        return (T) this;
    }

    static Integer toInt(Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof Number) {
            return ((Number) value).intValue();
        }
        return Integer.valueOf(value.toString());
    }

    static Long toLong(Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof Number) {
            return ((Number) value).longValue();
        }
        return Long.valueOf(value.toString());
    }

    static Float toFloat(Object value) {
        if (value == null) {
            return null;
        }
        if (value instanceof Number) {
            return ((Number) value).floatValue();
        }
        return Float.valueOf(value.toString());
    }


    static Boolean toBoolean(Object value) {
        if (value == null) {
            return null;
        }
        return Boolean.TRUE == value;
    }

    static boolean checkValueType(Object value) {
        if (value == null) {
            return true;
        }
        if (value instanceof Integer) {
            return true;
        } else if (value instanceof Long) {
            return true;
        } else if (value instanceof Boolean) {
            return true;
        } else if (value instanceof Float) {
            return true;
        } else if (value instanceof String) {
            return true;
        } else if (value instanceof Set) {
            return true;
        } else {
            throw new RuntimeException("不允许的类型");
        }
    }
}
